1919D - 01 Tree - CodeForces Solution


constructive algorithms data structures dsu greedy sortings trees *2100

Please click on ads to support us..

C++ Code:

/*#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx")*/
#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
//#define endl '\n'
#define int ll
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pcc pair <char, char>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define in insert
#define MP make_pair
#define sz(s) (int)s.size()
#define inf (ll)1e18

//typedef tree<int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 5e5 + 10;
const int mod = 998244353;

int n;
vector <int> a, pr, nxt;

bool good(int x)
{
    if(x < 1 || x > n)
        return false;
    return a[pr[x]] == a[x] - 1 || a[nxt[x]] == a[x] - 1;
}

void solve()
{
    cin >> n;
    a = pr = nxt = vector <int> (n + 2, -2);
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 0; i < n + 2; i++)
    {
        pr[i] = i - 1;
        nxt[i] = i + 1;
    }
    priority_queue <pii> pq;
    vector <int> u(n + 2, 0);
    for(int i = 1; i <= n; i++)
    {
        if(good(i))
        {
            pq.push({a[i], i});
            u[i] = 1;
        }
    }
    while(!pq.empty())
    {
        int x = pq.top().first, y = pq.top().second;
        pq.pop();
        pr[nxt[y]] = pr[y];
        nxt[pr[y]] = nxt[y];
        if(good(pr[y]) && u[pr[y]] == 0)
        {
            u[pr[y]] = 1;
            pq.push({a[pr[y]], pr[y]});
        }
        if(good(nxt[y]) && u[nxt[y]] == 0)
        {
            u[nxt[y]] = 1;
            pq.push({a[nxt[y]], nxt[y]});
        }
    }
    int cnt = 0, mn = inf;
    for(int i = 1; i <= n; i++)
    {
        mn = min(mn, a[i]);
        cnt += !u[i];
    }
    if(cnt == 1 && mn == 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return;
}

signed main()
{
    //freopen("br1.in", "r", stdin);
    //freopen("br1.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tc = 1;
    cin >> tc;
    while(tc--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm